package com.hzdh.account.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzdh.account.commom.exception.BusinessException;
import com.hzdh.account.entity.login.User;
import com.hzdh.account.repository.login.UserMapper;
import com.hzdh.account.request.login.UserRequest;
import com.hzdh.account.service.UserService;
import com.hzdh.account.utils.KeyUtils;

@Service
public class UserServiceImpl implements UserService {

	private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public User selectUserByConsition(UserRequest userRequest) {
		User user = new User();
		user.setUserAccount(userRequest.getUserAccount());
		user.setUserName(userRequest.getUserAccount());
		user.setPhone(userRequest.getUserAccount());
		user.setPassword(KeyUtils.getMD5(userRequest.getPassword()));
		User us = userMapper.queryUserByConsition(user );
		if (us == null) {
			throw new BusinessException("账号或者密码错误!");
		}
		
		return us;
	}

	@Override
	public User selectUserById(String userId) {
	
		return userMapper.selectUserByUserId(userId);
	}

	@Override
	public User querySessionUser() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return (User) request.getSession().getAttribute("userSession");
	}

	@Override
	public List<User> selectUser(UserRequest userRequest) {
		
		return null;
	}

	@Override
	public void addUserInfo(UserRequest userRequest) {
		if (userRequest == null) {
			throw new BusinessException("无数据");
		}
		
		User us = new User();
		us.setUserAccount(userRequest.getUserAccount());
		User u = userMapper.queryUserByConsition(us );
		User user = new User();
		BeanUtils.copyProperties(userRequest, user);
		if (u == null) {
			user.setUserId(UUID.randomUUID().toString().replaceAll("-", ""));
			// 设置默认密码
			user.setPassword(KeyUtils.getMD5("123456"));
			user.setIsActive("1");
			user.setCreateBy(querySessionUser().getUserId());
			user.setCreateTime(new Date());
			int count = userMapper.insertSelective(user );
			if (count == 0) {
				throw new BusinessException("新增用户失败!");
			}
		} else {
			user.setUserId(u.getUserId());
			int count = userMapper.updateByPrimaryKeySelective(user);
			if (count == 0) {
				throw new BusinessException("更新用户失败!");
			}
		}
	}

	@Override
	public PageInfo<User> getUserPage(Map<String, String> paramMap) {
		
		PageInfo<User> pageInfo = null;
		if (paramMap != null) {
			try {
				Integer pno=Integer.parseInt(String.valueOf(paramMap.get("page")));
				Integer pageSize=Integer.parseInt(String.valueOf(paramMap.get("limit")));
				PageHelper.startPage(pno, pageSize);
				
				List<User> user = userMapper.selectUser(paramMap);
				pageInfo = new PageInfo<>(user);
			} catch (Exception e) {
				logger.error("获取分页失败 = {}", e);
			}
		}
		return pageInfo;
	}

}
